#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/stat.h>
#include <errno.h>




int clientfd;
int sockfd;	
char *uart = "/dev/ttyUSB0";

int apply_net();	
void *xxx(void *arg);	
int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop);
int USART_send(char *buf);
		
/*unsigned char command_1[100] = {0xFF,0x02,0x04,0xfd,0x05};
unsigned char command_2[100] = {0xFF,0x02,0x04,0xfd,0x01};
unsigned char command_3[100] = {0xFF,0x02,0x04,0xfd,0x08};
unsigned char command_4[100] = {0xFF,0x02,0x05,0xfd,0x05};
unsigned char command_5[100] = {0xFF,0x02,0x05,0xfd,0x01};
unsigned char command_6[100] = {0xFF,0x02,0x05,0xfd,0x08};									//控制指令
unsigned char command_7[100] = {0xFF,0x02,0x07,0xfd,0x05};
unsigned char command_8[100] = {0xFF,0x02,0x07,0xfd,0x01};
unsigned char command_9[100] = {0xFF,0x02,0x07,0xfd,0x08};	*/
unsigned char command_d[8][5] = {0xFF,0x02,0x04,0xfd,0x01,
	                             0xFF,0x02,0x04,0xfd,0x02,
                                 0xFF,0x02,0x04,0xfd,0x03,
                                 0xFF,0x02,0x04,0xfd,0x04,
                                 0xFF,0x02,0x04,0xfd,0x05,
                                 0xFF,0x02,0x04,0xfd,0x06,
                                 0xFF,0x02,0x04,0xfd,0x07,
                                 0xFF,0x02,0x04,0xfd,0x08};
unsigned char command_q[8][5] = {0xFF,0x02,0x07,0xdc,0x01,
	                             0xFF,0x02,0x07,0xdc,0x02,
                                 0xFF,0x02,0x07,0xdc,0x03,
                                 0xFF,0x02,0x07,0xdc,0x04,
                                 0xFF,0x02,0x07,0xdc,0x05,
                                 0xFF,0x02,0x07,0xdc,0x06,
                                 0xFF,0x02,0x07,0xdc,0x07,
                                 0xFF,0x02,0x07,0xdc,0x08};
unsigned char command_s[8][5] = {0xFF,0x02,0x05,0xfd,0x01,
	                             0xFF,0x02,0x05,0xfd,0x02,
                                 0xFF,0x02,0x05,0xfd,0x03,
                                 0xFF,0x02,0x05,0xfd,0x04,
                                 0xFF,0x02,0x05,0xfd,0x05,
                                 0xFF,0x02,0x05,0xfd,0x06,
                                 0xFF,0x02,0x05,0xfd,0x07,
                                 0xFF,0x02,0x57,0xfd,0x08};
int main()
{
	apply_net();
	while(1)
	{
		clientfd = accept(sockfd, NULL, NULL);//recv call
		
		pthread_t t;
		pthread_create(&t, NULL, xxx, (void*)clientfd);
	}
}

void *xxx(void *arg)
{	
	int clientfd = (int)arg;
	
	while(1)
	{
		//4)接 read();
		char buf[100] = {0};
		static int i=4,j=7,k=7;
		int ret = read(clientfd, buf, 100);	
		if(0 == ret || -1 == ret)
			break;
		else
			printf("%s\n",buf);
		//USART_send(command_d[4]
		if(0 == strncmp("1", buf,1) )//左转
		{  
		   
			if(i>=7)
			{
				i=7;
			}
			else
			{
				printf("%d\n",i);
			
			}
			USART_send(command_d[++i]);
				printf("ok\n");
			//write(clientfd,command[1],sizeof(command[1]));
			write(clientfd,"1",1);
		}
		else if(0 == strncmp("2", buf,1) )//右转
		{
			
			if(i<=0)
			{
				i=0;
			}
			else
			{
				printf("%d\n",i);
			
			}
			USART_send(command_d[--i]);
			printf("ok\n");
			//write(clientfd,command[2],sizeof(command[2]));
			write(clientfd,"2",1);
		}
		
		else if(0 == strncmp("3", buf,1) )//前进
		{   
		   if(j<=0)
			{
				j=0;
			}
			else
			{
				printf("%d\n",j);
			
			}
			USART_send(command_q[--j]);
			printf("ok\n");
			//write(clientfd,command[3],sizeof(command[3]));
			write(clientfd,"3",1);
			
		}
		
		else if(0 == strncmp("4", buf,1) )//后退
		{
			if(j>=7)
			{
				j=7;
			}
			else
			{
				printf("%d\n",j);
			
			}
			USART_send(command_q[++j]);
			printf("ok\n");
			//write(clientfd,command[4],sizeof(command[4]));
			write(clientfd,"4",1);
		}
		
		else if(0 == strncmp("5", buf,1) )//升高
		{  
		  if(k<=0)
			{
				k=0;
			}
			else
			{
				printf("%d\n",k);
			
			}
			USART_send(command_s[--k]);
			printf("ok\n");
			//write(clientfd,command[0],sizeof(command[0]));
			write(clientfd,"5",1);
			

		}
		else if(0 == strncmp("6", buf,1) )//降低
		{
		
			if(k>=7)
			{
				k=7;
			}
			else
			{
				printf("%d\n",k);
			
			}
			USART_send(command_s[++k]);
			printf("ok\n");
			//write(clientfd,command[0],sizeof(command[0]));
			write(clientfd,"6",1);
		}
		/*else
			if(0 == strncmp("7", buf,1) )
		{
			USART_send(command_7);
			printf("ok\n");
			//write(clientfd,command[0],sizeof(command[0]));
			write(clientfd,"5",1);
		}
		else
			if(0 == strncmp("8", buf,1) )
		{
			USART_send(command_8);
			printf("ok\n");
			//write(clientfd,command[0],sizeof(command[0]));
			write(clientfd,"5",1);
		}
		else
			if(0 == strncmp("9", buf,1) )
		{
			USART_send(command_9);
			printf("ok\n");
			//write(clientfd,command[0],sizeof(command[0]));
			write(clientfd,"5",1);
		}*/
		else
			write(clientfd,buf,sizeof(buf));
		//3)write();
		//write(clientfd, "ok", 2);
	}

}

int apply_net()
{
	//1)申请网络功能 socket()
	sockfd = socket(AF_INET, SOCK_STREAM, 0);	
	if(-1 == sockfd)
	{
		perror("socket");
		return -1;
	}

	//2服务器wait for client
	struct sockaddr_in serveraddr = {0};
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_addr.s_addr = inet_addr("0.0.0.0");
	serveraddr.sin_port = htons(9999);
	int len = sizeof(serveraddr);
	if(-1 == bind(sockfd, (struct sockaddr*)&serveraddr, len) )
	{
		perror("bind");
		return -1;
	}	

	listen(sockfd, 10); 
}

int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
	struct termios newtio,oldtio;
	/* 获取fd串口对应的termios结构体，这步主要是查询串口是否启动正常 */
	if  ( tcgetattr( fd,&oldtio)  !=  0) { 
		perror("SetupSerial 1");
		return -1;
	}
	//清空
	bzero( &newtio, sizeof( newtio ) );
	newtio.c_cflag  |=  CLOCAL | CREAD;	//配置成本地模式(本地连接、不改变端口所有者)、可读
	newtio.c_cflag &= ~CSIZE;		//清空数据位设置
	/* 选择数据位 */
	switch( nBits )
	{
	case 7:
		newtio.c_cflag |= CS7;
		break;
	case 8:
		newtio.c_cflag |= CS8;	
		break;
	}
	/* 选择校验位 */
	switch( nEvent )
	{
	case 'O':
		newtio.c_cflag |= PARENB;
		newtio.c_cflag |= PARODD;
		newtio.c_iflag |= (INPCK | ISTRIP);	//启用输入奇偶检测、去掉第八位
		break;
	case 'E': 
		newtio.c_iflag |= (INPCK | ISTRIP);
		newtio.c_cflag |= PARENB;
		newtio.c_cflag &= ~PARODD;
		break;
	case 'N':  
		newtio.c_cflag &= ~PARENB;
		break;
	}
	/* 选择波特率 */
	switch( nSpeed )
	{
	case 2400:
		cfsetispeed(&newtio, B2400);
		cfsetospeed(&newtio, B2400);
		break;
	case 4800:
		cfsetispeed(&newtio, B4800);
		cfsetospeed(&newtio, B4800);
		break;
	case 9600:
		cfsetispeed(&newtio, B9600);
		cfsetospeed(&newtio, B9600);
		break;
	case 115200:
		cfsetispeed(&newtio, B115200);
		cfsetospeed(&newtio, B115200);
		break;
	case 460800:
		cfsetispeed(&newtio, B460800);
		cfsetospeed(&newtio, B460800);
		break;
	default:
		cfsetispeed(&newtio, B9600);
		cfsetospeed(&newtio, B9600);
		break;
	}
	/* 选择停止位，貌似linux下不能设置(1.5 0.5)停止位 */
	if( nStop == 1 )
		newtio.c_cflag &=  ~CSTOPB;
	else if ( nStop == 2 )
		newtio.c_cflag |=  CSTOPB;
	newtio.c_cc[VTIME]  = 0;
	newtio.c_cc[VMIN] = 0;
	tcflush(fd,TCIFLUSH);
	/* 设置新配置 */
	if((tcsetattr(fd,TCSANOW,&newtio))!=0)
	{
		perror("com set error");
		return -1;
	}
//	printf("set done!\n\r");
	return 0;
}

int USART_send(char *buf)
{
	int fd,wr_static;
	if((fd = open(uart, O_RDWR|O_NOCTTY|O_NDELAY))<0){
		return 0;
	}
	else{
		printf("open %s is success\n",uart);
		set_opt(fd,115200 , 8, 'N', 1); //设置串口
		wr_static = write(fd,buf, 5); 
		close(fd);	//释放串口设备资源
		return 1;
	}
}
